package com.example.controlorder2;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Comanda extends Activity {
	BDcomanda dbHelper=null;
	SQLiteDatabase db= null;
	
	ArrayAdapter<String> comandaAdapter=null;
	
protected void atualizarLista(){
	
		db=dbHelper.getReadableDatabase();
		comandaAdapter.clear();
		Cursor cursor=db.rawQuery("SELECT * FROM Comanda ORDER BY cd_mesa", null);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			String mesa= cursor.getString(1);
			
			comandaAdapter.add("Mesa " + mesa);

			cursor.moveToNext();
		}
		cursor.close();
		db.close();
		dbHelper.close();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comanda);
		
		ArrayList<String> comanda = new ArrayList<String>();
		comandaAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,comanda);
		ListView listViewComanda =(ListView)findViewById(R.id.listViewComanda);
		listViewComanda.setAdapter(comandaAdapter);
		try{
			dbHelper=new BDcomanda(getApplicationContext());
			atualizarLista();		
		}catch(Exception ex){
			new AlertDialog.Builder(this)
			.setMessage(ex.getMessage()).setPositiveButton("Ok",null).show();	
		}
        
		final Intent prod = new Intent(this,AlterarProduto.class);
        listViewComanda.setOnItemClickListener(new OnItemClickListener(){
           
        	public void onItemClick(AdapterView<?> disAdapterView, View v, int position, long id){
        		String produto = (String) disAdapterView.getAdapter().getItem(position);
        		prod.putExtra("nm_produto",produto);
        		
        		startActivity(prod);
        	}		
        });
        	
        }

	public void cadComanda(View view){
		Intent comanda = new Intent(this, CadComanda.class);
		startActivity(comanda);
	}
	public void voltarComanda (View view){
		Intent prod = new Intent(this,MainActivity.class);
		startActivity(prod);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.comanda, menu);
		return true;
	}

}